package com.lv.common.dialog

import android.content.Context
import android.widget.ImageView
import android.widget.SeekBar
import com.lv.common.R
import com.lv.common.base.BaseDialog
import com.lv.common.imp.OnDialogSureListener
import com.lv.common.widget.swipecaptcha.SwipeCaptchaView

/**
 * 作者： ygx
 * 创建日期：2022/5/18 9:53 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * _              _           _     _   ____  _             _ _
 * / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class SwipeCatpchaDialog : BaseDialog {
    private var sCapt: SwipeCaptchaView? = null
    private var dragBar: SeekBar? = null
    private var btnChange: ImageView? = null

    constructor(mContext: Context?, isCancelable: Boolean, isBackCancelable: Boolean) : super(
        mContext,
        isCancelable,
        isBackCancelable
    ) {
    }

    constructor(
        mContext: Context?,
        isCancelable: Boolean,
        isBackCancelable: Boolean,
        onDialogSureListener: OnDialogSureListener?
    ) : super(mContext, isCancelable, isBackCancelable) {

        this.onDialogSureListener = onDialogSureListener
    }

    constructor(
        mContext: Context?,
        isCancelable: Boolean,
        isBackCancelable: Boolean,
        gravity: Int,
        widthIsMatch: Boolean,
        heightIsMatch: Boolean
    ) : super(mContext, isCancelable, isBackCancelable, gravity, widthIsMatch, heightIsMatch) {
    }

    override fun initView() {
        sCapt = findViewById(R.id.s_capt)
        dragBar = findViewById(R.id.dragBar)
        btnChange = findViewById(R.id.btnChange)
        initEvnet()
    }

    private fun initEvnet() {
//        tv_sure!!.setOnClickListener {
//            if (onDialogSureListener != null) {
//                onDialogSureListener!!.onSureClick("")
//            }
//            dismiss()
//        }
//        tv_cancel!!.setOnClickListener { dismiss() }
        btnChange?.setOnClickListener {
            sCapt?.createCaptcha()
            dragBar?.isEnabled = true
            dragBar?.progress = 0
        }
        sCapt?.onCaptchaMatchCallback = object : SwipeCaptchaView.OnCaptchaMatchCallback {
            override fun matchSuccess(swipeCaptchaView: SwipeCaptchaView?) {
//                showToast("恭喜你啊 验证成功 可以搞事情了")
                dragBar?.isEnabled = false
                btnChange?.isEnabled=false
                onDialogSureListener?.onSureClick("")
                dismiss()
            }

            override fun matchFailed(swipeCaptchaView: SwipeCaptchaView?) {
//                showToast("你有80%的可能是机器人，现在走还来得及")
                sCapt?.createCaptcha()
                dragBar?.progress = 0
            }
        }
        dragBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(
                seekBar: SeekBar?,
                progress: Int,
                fromUser: Boolean
            ) {
                sCapt?.setCurrentSwipeValue(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                //随便放这里是因为控件
                dragBar?.max = sCapt?.maxSwipeValue ?: 0
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                sCapt?.matchCaptcha()
            }
        })
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_swipecapt
    }

    private var onDialogSureListener: OnDialogSureListener? = null
    fun setOnDialogSureListener(onDialogSureListener: OnDialogSureListener?) {
        this.onDialogSureListener = onDialogSureListener
    }

}